﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TheOutfield.UmbExt.DocTypeExtensions.Actions;
using umbraco.BusinessLogic;
using umbraco.cms.presentation.Trees;
using umbraco.BusinessLogic.Actions;

namespace TheOutfield.UmbExt.DocTypeExtensions
{
    public class AppBase : ApplicationBase
    {
        public AppBase()
        {
            BaseTree.BeforeNodeRender += new BaseTree.BeforeNodeRenderEventHandler(BaseTree_BeforeNodeRender);
        }

        protected void BaseTree_BeforeNodeRender(ref XmlTree sender, ref XmlTreeNode node, EventArgs e)
        {
            if (node.Menu != null && node.NodeType == "nodeTypes")
            {
                node.Menu.Insert(1, ContextMenuSeperator.Instance);
                node.Menu.Insert(2, SwapMasterDocTypeAction.Instance);
                node.Menu.Insert(3, ExtractMasterDocTypeAction.Instance);
            }

            //Add Open associated doctype when rendering content tree and when the current user has access to the settings tab
            if (node.Menu != null && node.NodeType == "content" && User.GetCurrent().Applications.Any(app => app.alias == "settings"))
            {
                //Insert open associated doctype action
                node.Menu.Add(ContextMenuSeperator.Instance);
                node.Menu.Add(OpenAssociatedDocType.Instance);
            }
        }
    }
}